package servlet.huwenyao;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import pojo.Goods;
import service.OrderService;
import service.impl.OrderServiceImpl;
import util.JsonDateValueProcessor;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(value = "/QueryWareServlet")
public class QueryWareServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置返回的格式
        response.setContentType("text/json;charset=UTF-8");
        //获取商品id
        String id[]= request.getParameter("wareId").split(",");
        OrderService orderService=new OrderServiceImpl();
        if(id.length!=0){
            List <Goods> goodsList =new ArrayList<Goods>();
            for(int i=0;i<id.length;i++){
                goodsList.add(orderService.finaGoods(Integer.parseInt(id[i])));
            }
            JSONObject json=new JSONObject();
            json.put("code",0);
            json.put("msg","");
            JsonConfig config=new JsonConfig();
            config.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());
            JSONArray array=JSONArray.fromObject(goodsList,config);
            json.put("data",array);
            PrintWriter out = response.getWriter();
            out.print(json);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doPost(request,response);
    }
}
